require'buffself'
Infopunk = class('Infopunk')

function Infopunk:initialize(ac_events)
	print("initalizing Infopunk")
	self.ac_events = setmetatable({}, {__index = ac_events})
	if not INFOPUNK_DEBUG then
		self.skapi = luacom.CreateObject("SkunkWorks.SkapiCls")
	else
		local get_skapi = luacom.CreateObject("SkunkWorks.GetSkapiCls")
		self.skapi = get_skapi.skapi
	end

	if not self.skapi then
		print("infopunk: skapi not found")
		self:exit(1)
	else
		print("skapi initialized")
	end
	self.buffself = Buffself:new(self)
	self.cmd_prefix = "cmd"
	
	print("Infopunk initialized")

end

function Infopunk:set_player(player)
	self.player = player
end

function Infopunk:check_casting_vitals()
	print("checking casting vitals")
	local current_stamina = self.skapi.staminaCur	
	local stam_vital = self.skapi:SkinfoFromVital(3)
	local max_stamina = stam_vital.lvlCur
	print("stamina : " .. self.skapi.staminaCur .. " max : " .. max_stamina)

	local current_mana = self.skapi.manaCur	
	local mana_vital = self.skapi:SkinfoFromVital(5)
	local max_mana = mana_vital.lvlCur
	print("mana : " .. self.skapi.manaCur .. " max : " .. max_mana)
	

	local test_stamina = current_stamina/max_stamina
	local test_mana = current_mana/max_stamina
	print("stamina ratio: " .. test_stamina .. " stamina threshold: " .. self.player.stamina_threshold)
	print("mana ratio: " .. test_mana .. " mana threshold: " .. self.player.mana_threshold)

	if test_mana < self.player.mana_threshold then
		print("mana less than threshold, adding stamina to mana to queue")
		local buffer = {}
		buffer.name = self.player.stamina_to_mana
		buffer.target = nil
		table.insert(self.buffself.buff_queue, 1, buffer)
	end

	if test_stamina < self.player.stamina_threshold then
		print("stamina less than threshold, adding fill stamina to queue")
		local buffer = {}
		buffer.name = self.player.fill_stamina
		buffer.target = nil
		table.insert(self.buffself.buff_queue, 1,  buffer)
	end


end



function Infopunk:receive_command(sender, msg)

	local name = self.skapi.szName
	print("on tell fellowship " .. name .. " .. " .. sender)
	if sender == "" then
		print("sent by me")
		local prefix = msg:sub(1,3)
		if prefix == self.cmd_prefix then
			print("prefix == cmd")
			local cmd_type = msg:sub(5,8)			
			print("cmd_type == " .. cmd_type)
			local cmd_suffix = msg:sub(10)

			local commands = {}
			commands["buff"] = function(cmd_suffix)
					print("received buff command")
					print("player " .. self.player.name)					
					self.buffself:cmd(cmd_suffix, self.player)					
				end

			if commands[cmd_type] then
				print("command type valid")
				commands[cmd_type](cmd_suffix)
			end

			
		end
	end

end

function Infopunk:cmd(cmd, name)

	print("command " .. cmd .. " sent by " .. name)

end

function Infopunk:exit(code)
	print("exiting infopunk")
	self.skapi = nil
	collectgarbage()
	os.exit(code)

end